﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class PhieuNhapDCChiTietBO : Csla.BusinessBase<PhieuNhapDCChiTietBO>
    {
        private Hashtable hstOldValue = new Hashtable();
        public Hashtable HstOldValue
        {
            get { return this.hstOldValue; }
            set { this.hstOldValue = value; }
        }

        #region Business Properties

        private int? _PhieuNhapDCChiTietID = null;
        public int? PhieuNhapDCChiTietID
        {
            get { return this._PhieuNhapDCChiTietID; }
            set { this._PhieuNhapDCChiTietID = value; PropertyHasChanged("PhieuNhapDCChiTietID"); }
        }

        private int? _PhieuNhapDCID = null;
        public int? PhieuNhapDCID
        {
            get { return this._PhieuNhapDCID; }
            set { this._PhieuNhapDCID = value; PropertyHasChanged("PhieuNhapDCID"); }
        }

        private int? _DungCuID = null;
        public int? DungCuID
        {
            get { return this._DungCuID; }
            set { this._DungCuID = value; PropertyHasChanged("DungCuID"); }
        }

        private string _KyMaHieu = null;
        public string KyMaHieu
        {
            get { return this._KyMaHieu; }
            set { this._KyMaHieu = value; PropertyHasChanged("KyMaHieu"); }
        }

        private string _TenDungCu = null;
        public string TenDungCu
        {
            get { return this._TenDungCu; }
            set { this._TenDungCu = value; PropertyHasChanged("TenDungCu"); }
        }

        private int? _DonViTinhID = null;
        public int? DonViTinhID
        {
            get { return this._DonViTinhID; }
            set { this._DonViTinhID = value; PropertyHasChanged("DonViTinhID"); }
        }

        private string _TenDonViTinh = null;
        public string TenDonViTinh
        {
            get { return this._TenDonViTinh; }
            set { this._TenDonViTinh = value; PropertyHasChanged("TenDonViTinh"); }
        }

        private double? _SoLuong = null;
        public double? SoLuong
        {
            get { return this._SoLuong; }
            set { this._SoLuong = value; PropertyHasChanged("SoLuong"); }
        }

        private double? _DonGia = null;
        public double? DonGia
        {
            get { return this._DonGia; }
            set { this._DonGia = value; PropertyHasChanged("DonGia"); }
        }

        private double? _ThanhTien = null;
        public double? ThanhTien
        {
            get { return this._ThanhTien; }
            set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _PhieuNhapDCChiTietID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newPhieuNhapDCChiTietID = -1;
        private PhieuNhapDCChiTietBO()
        {
            _PhieuNhapDCChiTietID = newPhieuNhapDCChiTietID--;
        }

        public PhieuNhapDCChiTietBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public PhieuNhapDCChiTietBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static PhieuNhapDCChiTietBO NewRootPhieuNhapDCChiTietBO()
        {
            return DataPortal.Create<PhieuNhapDCChiTietBO>(new RootCriteria());
        }

        internal static PhieuNhapDCChiTietBO NewChildPhieuNhapDCChiTietBO()
        {
            return DataPortal.Create<PhieuNhapDCChiTietBO>(new ChildCriteria());
        }

        public static PhieuNhapDCChiTietBO GetRootPhieuNhapDCChiTietBO(int _PhieuNhapDCChiTietID)
        {
            return DataPortal.Fetch<PhieuNhapDCChiTietBO>(new RootCriteria(_PhieuNhapDCChiTietID));
        }

        public static PhieuNhapDCChiTietBO GetRootPhieuNhapDCChiTietBOByWhere(string where)
        {
            return DataPortal.Fetch<PhieuNhapDCChiTietBO>(where);
        }

        internal static PhieuNhapDCChiTietBO GetChildPhieuNhapDCChiTietBO(SqlDataReader dataReader)
        {
            return new PhieuNhapDCChiTietBO(dataReader);
        }

        public static void DeletePhieuNhapDCChiTietBO(int _PhieuNhapDCChiTietID)
        {
            DataPortal.Delete(new RootCriteria(_PhieuNhapDCChiTietID));
        }

        public static void DeletePhieuNhapDCChiTietBO(int _PhieuNhapDCChiTietID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_PhieuNhapDCChiTietID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int PhieuNhapDCChiTietID;
            public bool directDelete = false;
            public RootCriteria(int _PhieuNhapDCChiTietID)
            {
                this.PhieuNhapDCChiTietID = _PhieuNhapDCChiTietID;
                this.directDelete = false;
            }

            public RootCriteria(int _PhieuNhapDCChiTietID, bool directDelete)
            {
                this.PhieuNhapDCChiTietID = _PhieuNhapDCChiTietID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int PhieuNhapDCChiTietID;
            public bool directDelete = false;
            public ChildCriteria(int _PhieuNhapDCChiTietID)
            {
                this.PhieuNhapDCChiTietID = _PhieuNhapDCChiTietID;
                this.directDelete = false;
            }

            public ChildCriteria(int _PhieuNhapDCChiTietID, bool directDelete)
            {
                this.PhieuNhapDCChiTietID = _PhieuNhapDCChiTietID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuNhapDCChiTietBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[PhieuNhapDCChiTietID] = " + criteria.PhieuNhapDCChiTietID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuNhapDCChiTietBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["PhieuNhapDCChiTietID"] is DBNull))
                this._PhieuNhapDCChiTietID = (int)dataReader["PhieuNhapDCChiTietID"];
            if (!(dataReader["PhieuNhapDCID"] is DBNull))
                this._PhieuNhapDCID = (int)dataReader["PhieuNhapDCID"];
            if (!(dataReader["DungCuID"] is DBNull))
                this._DungCuID = (int)dataReader["DungCuID"];
            if (!(dataReader["KyMaHieu"] is DBNull))
                this._KyMaHieu = (string)dataReader["KyMaHieu"];
            if (!(dataReader["TenDungCu"] is DBNull))
                this._TenDungCu = (string)dataReader["TenDungCu"];
            if (!(dataReader["DonViTinhID"] is DBNull))
                this._DonViTinhID = (int)dataReader["DonViTinhID"];
            if (!(dataReader["TenDonViTinh"] is DBNull))
                this._TenDonViTinh = (string)dataReader["TenDonViTinh"];
            if (!(dataReader["SoLuong"] is DBNull))
                this._SoLuong = (double)dataReader["SoLuong"];
            if (!(dataReader["DonGia"] is DBNull))
                this._DonGia = (double)dataReader["DonGia"];
            if (!(dataReader["ThanhTien"] is DBNull))
                this._ThanhTien = (double)dataReader["ThanhTien"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

            GetOldData();
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuNhapDCChiTietBO_InsertBO");
            db.AddInParameter(cmd, "@PhieuNhapDCID", SqlDbType.Int, this._PhieuNhapDCID);
            db.AddInParameter(cmd, "@DungCuID", SqlDbType.Int, this._DungCuID);
            db.AddInParameter(cmd, "@KyMaHieu", SqlDbType.NVarChar, this._KyMaHieu);
            db.AddInParameter(cmd, "@TenDungCu", SqlDbType.NVarChar, this._TenDungCu);
            db.AddInParameter(cmd, "@DonViTinhID", SqlDbType.Int, this._DonViTinhID);
            db.AddInParameter(cmd, "@TenDonViTinh", SqlDbType.NVarChar, this._TenDonViTinh);
            db.AddInParameter(cmd, "@SoLuong", SqlDbType.Float, this._SoLuong);
            db.AddInParameter(cmd, "@DonGia", SqlDbType.Float, this._DonGia);
            db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewPhieuNhapDCChiTietID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._PhieuNhapDCChiTietID = (int)cmd.Parameters["@NewPhieuNhapDCChiTietID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuNhapDCChiTietBO_UpdateBO");
            db.AddInParameter(cmd, "@PhieuNhapDCChiTietID", SqlDbType.Int, this._PhieuNhapDCChiTietID);
            db.AddInParameter(cmd, "@PhieuNhapDCID", SqlDbType.Int, this._PhieuNhapDCID);
            db.AddInParameter(cmd, "@DungCuID", SqlDbType.Int, this._DungCuID);
            db.AddInParameter(cmd, "@KyMaHieu", SqlDbType.NVarChar, this._KyMaHieu);
            db.AddInParameter(cmd, "@TenDungCu", SqlDbType.NVarChar, this._TenDungCu);
            db.AddInParameter(cmd, "@DonViTinhID", SqlDbType.Int, this._DonViTinhID);
            db.AddInParameter(cmd, "@TenDonViTinh", SqlDbType.NVarChar, this._TenDonViTinh);
            db.AddInParameter(cmd, "@SoLuong", SqlDbType.Float, this._SoLuong);
            db.AddInParameter(cmd, "@DonGia", SqlDbType.Float, this._DonGia);
            db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_PhieuNhapDCChiTietID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuNhapDCChiTietBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@PhieuNhapDCChiTietID", SqlDbType.Int, criteria.PhieuNhapDCChiTietID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        private void GetOldData()
        {
            Type t = this.GetType();
            MemberInfo[] mi = t.GetMembers();
            int length = mi.Length;
            string name = "";
            object objValue = null;
            string strValue = null;

            for (int i = 0; i < length; i++)
                if (mi[i].MemberType == MemberTypes.Property)
                {
                    name = mi[i].Name;
                    if (StaticUtil.Log_ValidColumn(name))
                    {
                        objValue = t.GetProperty(name).GetValue(this, null);
                        if (objValue != null)
                            strValue = objValue.ToString();
                        else
                            strValue = "";

                        hstOldValue.Add(name, strValue);
                    }
                }
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_PhieuNhapDCChiTietID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
